<?php

namespace app\models;

use app\models\CommicRecord;
use yii\db\ActiveRecord;
use Yii;

class CommicRecord extends ActiveRecord{
	
	
	public static function tableName(){
		// return "shop_admin";
		return "{{%CommicRecord}}";
	}
	
	//uId 用户序号	uName 用户昵称	uMail 用户邮箱	uNumber 用户账号	uPassword 密码	uHeadurl 头像 state

	public function rules(){
        return [
            [['uId', 'cId'], 'required'],
        ];
    }

    public function attributeLabels(){
        return [
            'uId' => '用户id',
            'cId' => '漫画id',
            'uCReading' => '已阅读的历史', //01/10：第一集第10话
            'uCUpdatetime' => '时间'
        ];
    }
    
    //插入数据&更改数据
    //同一部漫画有可能刚读或者继续阅读
    public function add($data){
        //load是php框架内部自主加载数据的函数，而validate是自主验证rules
        $user = new CommicRecord();
        $user->uId = $data['uId'];
        $user->cId = $data['cId'];
        $user->uCReading = $data['uCReading'];
        if ($user->save()) {
            return true;
        }
        return false;
    }

    //某部漫画阅读历史查询
    public function getHisByuIdcId($uId, $cId){
    	$data = self::find()->select(['cId','uCReading'])->where('uId = :uId and cId = :cId',[':uId'=>$uId, ':cId'=>$cId])->asArray()->one();
    	return $data;
    }

    //查看漫画全部历史
    public function getAllByuId($uId){
    	$data = self::find()->select(['cId','uCReading'])->where('uId = :uId',[':uId'=>$uId])->orderBy('uCUpdatetime desc')->asArray()->all();
    	return $data;
    }

    //更新用户阅读历史
    public function updateCReading($data){
    	$key = [
        	'uId' => $data['uId'],
            'cId' => $data['cId'],
        ];
        $changedata = [
            'uCReading' => $data['uCReading'],
        ];
        $result = CommicRecord::updateAll($changedata,$key);
        if( $result == 1 ){
            return true;
        }
        return false;
    }
}
